package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.h3c.nem.dto.TbOfferLanguageDTO;
import com.h3c.nem.mapper.TbOfferLanguageMapper;
import com.h3c.nem.entity.TbOfferLanguage;
import com.h3c.nem.mapstruct.TbOfferLanguageMapstruct;
import com.h3c.nem.service.ITbOfferLanguageService;
import com.h3c.nem.utils.CommonUtil;
import com.h3c.nem.vo.TbOfferLanguageVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 语言能力实现类
 *
 * @author chenyifan
 * @since 2022-06-17 11:31:12
 */
@Service
public class TbOfferLanguageServiceImpl extends ServiceImpl<TbOfferLanguageMapper, TbOfferLanguage> implements ITbOfferLanguageService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOfferLanguage(List<TbOfferLanguageDTO> languageDTOS) {
        String workerId = CommonUtil.getActivUser().getUserId();
        Date now = new Date();
        remove(new QueryWrapper<TbOfferLanguage>().lambda()
                .eq(TbOfferLanguage::getWorkerId,workerId)
        );
        List<TbOfferLanguage> languageList = TbOfferLanguageMapstruct.INSTANCE.dtoList2EntityList(languageDTOS);
        for (TbOfferLanguage tbOfferLanguage : languageList) {
            tbOfferLanguage.setLanguageId(IdWorker.nextId()+"")
                    .setWorkerId(workerId)
                    .setUpdateTime(now);
            if(tbOfferLanguage.getLanguageType().contains("英语")){
                tbOfferLanguage.setEngMark("1");
            }else{
                tbOfferLanguage.setEngMark("0");
            }
        }
        saveBatch(languageList);
    }

    @Override
    public List<TbOfferLanguageVO> showOfferLanguage() {
        List<TbOfferLanguage> tbOfferLanguages = list(new QueryWrapper<TbOfferLanguage>().lambda()
                .eq(TbOfferLanguage::getWorkerId, CommonUtil.getActivUser().getUserId())
        );
        return TbOfferLanguageMapstruct.INSTANCE.entityList2VOList(tbOfferLanguages);
    }

    @Override
    public List<TbOfferLanguage> getOfferLanguageByWorkerId(String workerId) {
        LambdaQueryWrapper<TbOfferLanguage> queryWrapper = new QueryWrapper<TbOfferLanguage>().lambda()
                .eq(TbOfferLanguage::getWorkerId, workerId);
        return list(queryWrapper);
    }
}

